package com.cocolove2.library_cocodialog.dialogs;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cocolove2.library_cocodialog.CornerUtils;
import com.cocolove2.library_cocodialog.dialogs.base.CocoListDialogBase;
import com.cocolove2.library_cocodialog.entity.DialogMenuItem;

/**
 * 列表选择对话框
 */
public class NormalListCocoDialog extends CocoListDialogBase<NormalListCocoDialog> {

    public NormalListCocoDialog(Context context) {
        super(context);
        init();
    }

    public NormalListCocoDialog(Context context, int style) {
        super(context, style);
        init();
    }

    private void init() {
        /** LayoutAnimation */
        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF,
                0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0);
        animation.setInterpolator(new DecelerateInterpolator());
        animation.setDuration(550);

        mLac = new LayoutAnimationController(animation, 0.12f);
        mLac.setInterpolator(new DecelerateInterpolator());
    }

    @Override
    protected View onCreateView(Context context, ViewGroup parent) {
        LinearLayout ll_container = new LinearLayout(mContext);
        ll_container.setOrientation(LinearLayout.VERTICAL);
        ll_container.setLayoutParams(getDefaultparams());
        ll_container.setClickable(true);
        /** title */
        mTvTitle = new TextView(mContext);
        mTvTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        mTvTitle.setSingleLine(true);
        mTvTitle.setPadding(dp2px(18), dp2px(10), 0, dp2px(10));

        ll_container.addView(mTvTitle);

        /** listview */
        mLv = new ListView(mContext);
        mLv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        mLv.setCacheColorHint(Color.TRANSPARENT);
        mLv.setFadingEdgeLength(0);
        mLv.setVerticalScrollBarEnabled(false);
        mLv.setSelector(new ColorDrawable(Color.TRANSPARENT));

        ll_container.addView(mLv);
        return ll_container;
    }

    @Override
    protected void setupUiView() {
        super.setupUiView();
        /** title */
        mTvTitle.setBackground(CornerUtils.cornerDrawable(mTitleBgColor, new float[]{mCornerRadius, mCornerRadius, mCornerRadius,
                mCornerRadius, 0, 0, 0, 0}));
        mTvTitle.setText(mTitle);
        mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTitleTextSize);
        mTvTitle.setTextColor(mTitleTextColor);
        mTvTitle.setVisibility(mTitle != null ? View.VISIBLE : View.GONE);

        /** listview */
        mLv.setDivider(new ColorDrawable(mDividerColor));
        mLv.setDividerHeight(dp2px(mDividerHeight));
        mLv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        if (mTitle != null) {
            mLv.setBackground(CornerUtils.cornerDrawable(mDialogBgColor, new float[]{0, 0, 0, 0, mCornerRadius, mCornerRadius, mCornerRadius,
                    mCornerRadius}));
        } else {
            mLv.setBackground(CornerUtils.cornerDrawable(mDialogBgColor, mCornerRadius));
        }

        if (mAdapter == null) {
            mAdapter = new ListDialogAdapter();
        }

        mLv.setAdapter(mAdapter);
        mLv.setOnItemClickListener(mOnItemClickListener);

        mLv.setLayoutAnimation(mLac);

    }

    @Override
    protected View getAdapterView(int position, View convertView, ViewGroup parent) {
        final DialogMenuItem item = mContents.get(position);

        LinearLayout llItem = new LinearLayout(mContext);
        llItem.setOrientation(LinearLayout.HORIZONTAL);
        llItem.setGravity(Gravity.CENTER_VERTICAL);

        ImageView ivItem = new ImageView(mContext);
        ivItem.setPadding(0, 0, dp2px(15), 0);
        llItem.addView(ivItem);

        TextView tvItem = new TextView(mContext);
        tvItem.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        tvItem.setSingleLine(true);
        tvItem.setTextColor(mItemTextColor);
        tvItem.setTextSize(TypedValue.COMPLEX_UNIT_SP, mItemTextSize);

        llItem.addView(tvItem);
        float radius = dp2px(mCornerRadius);
        if (mTvTitle.getVisibility() == View.VISIBLE) {
            llItem.setBackground((CornerUtils.listItemSelector(radius, Color.TRANSPARENT, mItemPressColor,
                    position == mContents.size() - 1)));
        } else {
            llItem.setBackground(CornerUtils.listItemSelector(radius, Color.TRANSPARENT, mItemPressColor,
                    mContents.size(), position));
        }

        int left = item.mResId == 0 ? dp2px(18) : dp2px(16);
        int top = dp2px(10);
        int right = 0;
        int bottom = dp2px(10);
        llItem.setPadding(left + mItemExtraLeft, top + mItemExtraTop, right + mItemExtraRight, bottom + mItemExtraBottom);

        ivItem.setImageResource(item.mResId);
        tvItem.setText(item.mOperName);
        ivItem.setVisibility(item.mResId == 0 ? View.GONE : View.VISIBLE);

        return llItem;
    }
}
